package ar.com.android.alco.forms;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.android.alco.adapters.ListViewCreditosAdapter;
import ar.com.android.alco.dbHelpers.SQLALCOHelper;
import ar.com.android.alco.entities.Alimentos;
import ar.com.android.alco.providers.AlimentosProvider;

public class CreditosActivity extends Activity {
	private static final String TAG = "CreditosActivity";
	
	private ListViewCreditosAdapter listViewCreditosAdapter;
	private List<Alimentos> alimentos = new ArrayList<Alimentos>();
	Alimentos selectedAlimento;
	
	TextView tituloActivity;
	EditText etDescripcion;
	ImageButton butSearch;
	Button butVerDetalle;
	ListView lvCreditos;
	
    @Override
    public void onStart()
    {
    	super.onStart();
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.creditos);
        
        tituloActivity = (TextView) findViewById(R.id.txtTitulo);
        lvCreditos = (ListView) findViewById(R.id.lvCreditos);
        etDescripcion = (EditText) findViewById(R.id.etDescripcion);
        
        SpannableString content = new SpannableString(getString(R.string.BusquedaCreditos));
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        tituloActivity.setText(content);
        
        SQLALCOHelper sqlHelper = SQLALCOHelper.getHelper(CreditosActivity.this);
        sqlHelper.dbOpen();
        
        try {
    		listViewCreditosAdapter = new ListViewCreditosAdapter(this, R.id.lvCreditos, alimentos);
    		View header = getLayoutInflater().inflate(R.drawable.header_creditos, null);
    		lvCreditos.addHeaderView(header);
    		lvCreditos.setAdapter(listViewCreditosAdapter);

    		lvCreditos.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					
					for(Alimentos alimentoItem: alimentos)
						alimentoItem.setSelected(false);
					
					selectedAlimento = alimentos.get(position - 1);
					selectedAlimento.setSelected(true);
					listViewCreditosAdapter.notifyDataSetChanged();
				}
			});
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
        finally {
        	sqlHelper.dbClose();
        }
    }
    
    public void onClickDetalle(View v) throws Exception
    {
    	if(selectedAlimento != null) {
			Intent intentDetalle = new Intent(this, CreditosDetalleActivity.class);
			intentDetalle.putExtra("selectedAlimento", selectedAlimento.getIdAlimento());
			startActivityForResult(intentDetalle, 1);
    	}
    }
    
    public void onClickBuscar(View v) throws Exception
    {
    	SQLALCOHelper sqlHelper = SQLALCOHelper.getHelper(CreditosActivity.this);
    	
    	if(etDescripcion.getText().toString().equals("")) {
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.drawable.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

			TextView text = (TextView) layout.findViewById(R.id.text);
			text.setText(getText(R.string.msgInputDescripcion));

			Toast toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();
			
			etDescripcion.requestFocus();
    	}
    	else {
            sqlHelper.dbOpen();
            
            try {
            	
            	AlimentosProvider alimentosProvider = new AlimentosProvider(this);
            	alimentos = alimentosProvider.getAlimentos(etDescripcion.getText().toString());
            	
            	listViewCreditosAdapter.clear();
            	listViewCreditosAdapter.notifyDataSetChanged();
            	
            	listViewCreditosAdapter = new ListViewCreditosAdapter(this, R.id.lvCreditos, alimentos);
        		lvCreditos.setAdapter(listViewCreditosAdapter);
        		listViewCreditosAdapter.notifyDataSetChanged();
        		
    		} catch (Exception e) {
    			Log.e(TAG, e.getMessage());
    		}
            finally {
            	sqlHelper.dbClose();
            }	
    	}
    }
}